package com.hr.web;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hr.pojo.Admin;
import com.hr.service.AdminService;
import com.hr.service.ApplicantService;
import com.hr.service.impl.AdminServiceImpl;
import com.hr.service.impl.ApplicantServiceImpl;
import com.hr.utils.DateUtil;
import com.hr.utils.StringUtil;
public class AdminServlet extends BaseServlet {
	private AdminService adminService=new AdminServiceImpl();
	private ApplicantService applicantService=new ApplicantServiceImpl();
	//管理员登录
	public String adminLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
		//接受用户名和用户密码
		String username=request.getParameter("username");
		String password=request.getParameter("password");

		//MD5加密密码
		password=StringUtil.stringToMD5(password);
		
		//调用业务层功能 : 根据用户名查找用户  select * from user where username=?, 返回用户
		Admin admin=adminService.findUserByUsernameAndPassword(username,password);
		//根据返回的用户是否为空,判断用户是否已经存在  ,向客户端响应		
		if(null!=admin){
			
			response.setContentType("text/html;charset=utf-8");   
			request.getRequestDispatcher("/public/alert.jsp").include(request, response);
			PrintWriter out = response.getWriter();
			if(admin.getPassword().equals(password)) {
				request.getSession().setAttribute("admin",admin);
				out.println("<script>ok_alert(\"登录成功\",\"main.jsp\");</script>");
			}else {
				out.println("<script>no_alert(\"登录失败,密码错误！\",\"index.jsp\");</script>");
			}
			
		}else{
			response.setContentType("text/html;charset=utf-8");   
			request.getRequestDispatcher("/index.jsp").include(request, response);  
	        PrintWriter out = response.getWriter();
	        out.println("<script>no_alert(\"登录失败,找不到该用户！\",\"index.jsp\");</script>");
		}
		return null;
	}
	//退出
	public String adminLogout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
		request.getSession().removeAttribute("admin");
		response.setContentType("text/html;charset=utf-8");   
		request.getRequestDispatcher("/main.jsp").include(request, response);
        PrintWriter out = response.getWriter();
        out.println("<script>ok_alert(\"退出成功\",\"index.jsp\");</script>");
		return null;
	}
	public String toAddPage(HttpServletRequest request, HttpServletResponse response) throws Exception	{
		response.setCharacterEncoding("UTF-8");
		request.getRequestDispatcher("/admin/adduser.jsp").include(request, response);
		return null;
		
	}
	//管理员添加
	public String adminAdd(HttpServletRequest request, HttpServletResponse response) throws Exception{
		Admin admin = new Admin();
		admin.setUsername(request.getParameter("username"));
		admin.setPassword(StringUtil.stringToMD5(request.getParameter("password")));//加密
		admin.setSex(new Byte(request.getParameter("sex")));
		admin.setBirthday(DateUtil.parseToDate(request.getParameter("birthday"), DateUtil.yyyyMMdd));
		admin.setCreatetime(new java.util.Date());
		admin.setContent(request.getParameter("content"));
		String applicantId=request.getParameter("applicantId");
		admin.setIfadmin(new Byte("0"));
		if(applicantId!="") {admin.setIfadmin(new Byte("1"));}
		adminService.saveUser(admin);
		if(applicantId!="") {
			admin.setIfadmin(new Byte("1"));
			List adminList=adminService.findAllUsers();
			Admin u=(Admin)adminList.get(adminList.size() - 1);
			applicantService.updateadminApplicant(applicantId,u.getId().toString());
		};
		response.setContentType("text/html;charset=utf-8");  
		request.getRequestDispatcher("./public/alert.jsp").include(request, response);
        PrintWriter out = response.getWriter();
        //跳转到列表页面
        if(applicantId!="") {
        	out.println("<script>ok_alert(\"员工录用成功!\",\"main.jsp?method=findApplicantByIsstock&isstock=3&servlet=ApplicantServlet\");</script>");
        }else {
        	out.println("<script>ok_alert(\"添加成功!\",\"main.jsp?method=adminList&servlet=AdminServlet\");</script>");
        }
		return null;
	}
	//管理员list
	public List adminList(HttpServletRequest request, HttpServletResponse response) throws Exception{
		response.setContentType("text/html;charset=utf-8");
		List adminList=adminService.findAllUsers();
		request.setAttribute("adminList", adminList);
		request.getRequestDispatcher("/admin/listadmin.jsp").include(request, response);
		return null;
	}
	//更新管理员页面
	public String toUpdatePage(HttpServletRequest request, HttpServletResponse response) throws Exception	{
		String  id = request.getParameter("id");		
		Admin adminuser =adminService.findUserById(id);
		request.setAttribute("admin", adminuser);
		response.setContentType("text/html;charset=utf-8");
		request.getRequestDispatcher("/admin/updateuser.jsp").include(request, response);
		return null;
	}
	//更新管理员
	public String updateAdmin(HttpServletRequest request, HttpServletResponse response) throws Exception{
		Admin user = new Admin();
		user.setId(new Long(request.getParameter("id")));
		user.setUsername(request.getParameter("username"));
		user.setPassword(request.getParameter("password"));
		user.setSex(new Byte(request.getParameter("sex")));
		user.setBirthday(DateUtil.parseToDate(request.getParameter("birthday"), DateUtil.yyyyMMdd));
		//user.setCreatetime(new java.util.Date());
		user.setContent(request.getParameter("content"));		
		
		adminService.updateUser(user);
		List adminList=adminService.findAllUsers();
		request.setAttribute("adminList", adminList);

		response.setContentType("text/html;charset=utf-8");  
		request.getRequestDispatcher("./public/alert.jsp").include(request, response);
        PrintWriter out = response.getWriter();
        out.println("<script>ok_alert(\"修改成功\",\"main.jsp?method=adminList&servlet=AdminServlet\");</script>");
		return null;
	}
	//删除管理员
	public String deleteAdmin(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String id = request.getParameter("id");
		
		adminService.delete(id);
		response.setContentType("text/html;charset=utf-8");  
		request.getRequestDispatcher("./public/alert.jsp").include(request, response);
        PrintWriter out = response.getWriter();
        out.println("<script>ok_alert(\"删除成功\",\"main.jsp?method=adminList&servlet=AdminServlet\");</script>");
		return null;
	}
}
